<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title">算法新增</h4>
</div>
<form id="defaultForm" method="post" class="form-horizontal" action="/project/arithmeticManage/manage_save">
    <div class="modal-body">
        <input style="display: none" name="id" class="form-control" th:value="${id}">
        <div class="form-group">
            <label class="col-lg-1 control-label">名称</label>
            <div class="col-lg-5">
                <input type="text"th:value="${arithmeticManage.name}" class="form-control" name="name" />
            </div>

            <label class="col-lg-1 control-label">对象</label>
            <div class="col-lg-2">
                <select class="form-control" name="objName" id="objName">
                    <option value="">请选择</option>
                    <option th:each="parameter:${objects}" th:value="${parameter.remark}" th:text="${parameter.name}"
                            th:selected="${parameter.remark eq arithmeticManage.objName+''}"></option>
                </select>
            </div>

            <label class="col-lg-1 control-label">版本</label>
            <div class="col-lg-2">
                <input type="text" th:value="${arithmeticManage.version}" class="form-control" name="version" />
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-1 control-label">备注</label>
            <div class="col-lg-11">
                <textarea  style="height:60px" th:text="${arithmeticManage.remark}" class="form-control" name="remark" id="remark"></textarea>
            </div>
        </div>

        <div class="form-group">

            <label class="col-lg-2 control-label" style="text-align: left">输入输出</label>

            <label class="col-lg-2 control-label" style="text-align: center">输入量</label>
            <a class="col-lg-2 control-label" onclick="parameterData('/project/arithmeticManage/inParameter_info')" >编辑</a>

            <label class="col-lg-3 control-label" style="text-align: right">输出量</label>
            <a class="col-lg-2 control-label" onclick="parameterData('/project/arithmeticManage/outParameter_info')" >编辑</a>
        </div>

        <div class="form-group">
            <div class="col-lg-5" style="margin-left:75px;">
                <textarea  id="input" style="height:150px;" th:text="${arithmeticManage.inParameter}" class=" form-control"  disabled></textarea>
            </div>
            <div class="col-lg-5">
                <textarea  id="output"style="height:150px;"th:text="${arithmeticManage.outParameter}"  class=" form-control" disabled></textarea>
                <input type="hidden"  id="outputName" />
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-2 control-label" style="text-align: left">默认触发条件</label>
            <div class="col-lg-9">
                <textarea style="height:60px" class="form-control"th:text="${arithmeticManage.touchCondition}"
                          id="touchCondition" name="touchCondition" disabled></textarea>
            </div>
            <a style="margin-left: 10px" onclick="conditionData('/project/arithmeticManage/condition_info')">编辑</a>
        </div>

        <div class="form-group">
            <label class="col-lg-2 control-label" style="text-align: left">是否支持反向控制</label>
            <div class="col-lg-10">
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="isReverseControl" id="isReverseControl" th:checked="${arithmeticManage.isReverseControl eq 1 }"
                               th:value="${arithmeticManage.isReverseControl}" onclick="this.value=(this.value==0)?1:0">
                    </label>
                </div>
            </div>
        </div>

        <!--<div class="form-group">-->
            <!--<label class="col-lg-2 control-label" style="text-align: left">反向控制默认工序</label>-->
            <!--<div class="col-lg-10">-->

                <!--<table class="table table-striped table-bordered">-->
                    <!--<thead>-->
                    <!--<tr>-->
                        <!--<th>顺序</th>-->
                        <!--<th>操作</th>-->
                        <!--<th>时长(s)</th>-->
                        <!--<th>操作</th>-->
                    <!--</tr>-->
                    <!--</thead>-->
                    <!--<tbody>-->
                    <!--<tr>-->
                        <!--<td>1</td>-->
                        <!--<td>-->
                            <!--<select class="form-control" name="">-->
                                <!--<option th:each="parameter:${objects}" th:value="${parameter.id}" th:text="${parameter.name}"></option>-->
                            <!--</select>-->
                        <!--</td>-->
                        <!--<td>-->
                            <!--<input type="text" class="form-control" name="" />-->
                        <!--</td>-->
                        <!--<td>-->
                            <!--<span class="glyphicon glyphicon-chevron-up" aria-hidden="true" style="font-size:20px"></span>-->
                            <!--<span class="glyphicon glyphicon-chevron-down" aria-hidden="true" style="font-size:20px"></span>-->
                            <!--<span class="glyphicon glyphicon-plus-sign" aria-hidden="true" style="font-size:20px" ></span>-->
                            <!--<span class="glyphicon glyphicon-remove-sign" aria-hidden="true" style="font-size:20px" ></span>-->
                        <!--</td>-->
                    <!--</tr>-->
                    <!--<tr>-->
                        <!--<td>2</td>-->
                        <!--<td>-->
                            <!--<select class="form-control" name="object">-->
                                <!--<option th:each="parameter:${objects}" th:value="${parameter.id}" th:text="${parameter.name}"></option>-->
                            <!--</select>-->
                        <!--</td>-->
                        <!--<td><input type="text" class="form-control" name="" /></td>-->
                        <!--<td>-->
                            <!--<span class="glyphicon glyphicon-chevron-up" aria-hidden="true" style="font-size:20px"></span>-->
                            <!--<span class="glyphicon glyphicon-chevron-down" aria-hidden="true" style="font-size:20px"></span>-->
                            <!--<span class="glyphicon glyphicon-plus-sign" aria-hidden="true" style="font-size:20px"></span>-->
                            <!--<span class="glyphicon glyphicon-remove-sign" aria-hidden="true" style="font-size:20px"></span>-->
                        <!--</td>-->
                    <!--</tr>-->

                    <!--</tbody>-->
                <!--</table>-->
            <!--</div>-->
        <!--</div>-->

        <div class="form-group">
            <label class="col-lg-1 control-label">文件</label>
            <div class="col-lg-9">
                <input type="text" id="fileName" class="form-control" name="fileName" />
            </div>
            <div  class="col-lg-2">
                <button class="btn btn-success fileinput-button" type="button">打开</button>
                <input type="file" id="jobData" onchange="loadFile(this.files[0])" style="position:absolute;top:0;left:0;font-size:34px; opacity:0">
            </div>
        </div>


    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="submit" class="btn btn-primary">保存</button>
    </div>
</form>

<script>
    function loadFile(file){
        console.log(file.name);
        $("#fileName").val(file.name);
    }
function conditionData(url, id) {
    if (id != null) {
        url += "?id=" + id;
    }
    $("#modal_condition").modal({
        remote : url
    });
}

function parameterData(url, id) {

    if (id != null) {
        url += "?id=" + id;
    }

    $("#modal_parameter").modal({
        remote : url
    });
}

initCheck();

function initCheck() {
    $('#defaultForm')
        .bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                name: {
                    message: '不是有效的名称',
                    validators: {
                        notEmpty: {
                            message: '名称不能为空'
                        },
                        stringLength: {
                            min: 1,
                            max: 30,
                            message: '用户名不能超过30个字符'
                        }
                    }
                },
                version:{
                    message: '不是有效的版本',
                    validators: {
                        notEmpty: {
                            message: '版本不能为空'
                        },
                        stringLength: {
                            min: 1,
                            max: 30,
                            message: '用户名不能超过30个字符'
                        }
                    }
                },
                inParameter:{
                    message: '不是有效的输入参数',
                    validators: {
                        notEmpty: {
                            message: '输入参数不能为空'
                        }
                    }
                },
                outParameter:{
                    message: '不是有效的输出参数',
                    validators: {
                        notEmpty: {
                            message: '输出参数不能为空'
                        }
                    }
                },
                touchCondition:{
                    message: '不是有效的触发条件',
                    validators: {
                        notEmpty: {
                            message: '触发条件不能为空'
                        }
                    }
                }
            }
        })
        .on('success.form.bv', function(e) {
            // 阻止默认提交事件
            e.preventDefault();
            // 获取form表单
            var $form = $(e.target);
            // Get the BootstrapValidator instance
            var bv = $form.data('bootstrapValidator');
            console.log($('#defaultForm').serialize());

            var formData = new FormData();
            var file=$('#jobData')[0].files[0];
            var fileName=$("#fileName").val();

            formData.append("id",$("input[name='id']").val());
            formData.append("fileName",fileName);
            formData.append("file",file);
            formData.append("name", $("input[name='name']").val());
            formData.append("objName", $('#objName').find("option:selected").val());
            formData.append("remark", $("#remark").val());
            formData.append("version", $("input[name='version']").val());
            formData.append("inParameter",$("#input").val());
            formData.append("outParameter",$("#output").val());
            formData.append("outParameterName",$("#outputName").val());
            formData.append("touchCondition",$("#touchCondition").val());
            formData.append("isReverseControl",$("#isReverseControl").val());

            $.ajax({
                url: $form.attr('action'),
                type:"post",
                data:formData,
                contentType : false, //必须
                cache : false,//上传文件无需缓存
                processData : false,//用于对data参数进行序列化处理 这里必须false
                dataType:'json',
                success:function (result) {
                    if(result.type == "alert"){
                        alert(result.message);
                    }
                    $("#modal3").modal("hide");
                    //刷新跳转至第一页
//                        $("#dataTable").bootstrapTable("selectPage",1);
                    $("#dataTable").bootstrapTable('refresh', {url: '/project/arithmeticManage/manage_data'});
                },
                error:function () {
                    console.log("error")
                }
            });

        });
}

</script>